package com.chen.service.post;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chen.controller.admin.post.vo.*;
import com.chen.convert.post.PostConvert;
import com.chen.core.wrapper.LambdaQueryWrapperX;
import com.chen.dal.dateobject.post.PostDO;
import com.chen.dal.mysql.post.PostMapper;
import com.chen.enums.CommonStatus;
import com.chen.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.chen.constants.ErrorCodeConstant.POST_CODE_EXIST;
import static com.chen.constants.ErrorCodeConstant.POST_NOT_EXIST;
import static com.chen.utils.ServiceExpectionUtil.expection;

@Service
public class PostServiceImpl implements PostService{
    @Autowired
    private PostMapper postMapper;

    /**
     * 创建岗位
     * @param reqVO
     * @return
     */
    @Override
    public PostDO createPost(PostCreateReqVO reqVO) {
        //检验
        validatePostForCreateOrUpdate(
                null,reqVO.getCode(),reqVO.getName()
        );
        //转化
        PostDO postDO = PostConvert.CONVERT.convert01(reqVO);
        //初始化
        postDO.setStatus(CommonStatus.ENABLE.getStatus());
        //插入
        postMapper.insert(postDO);
        return postDO;
    }

    /**
     * 更新岗位
     * @param reqVO
     */
    @Override
    public void updatePost(PostUpdateReqVO reqVO) {
        //检验
        validatePostForCreateOrUpdate(
                reqVO.getId(), reqVO.getCode(),reqVO.getName()
        );
        //转化
       PostDO postObject= PostConvert.CONVERT.convert01(reqVO);
       //更新
        postMapper.updateById(postObject);
    }

    /**
     * 删除岗位
     * @param id
     */
    @Override
    public void deletePost(Long id) {
        //检验
        validatePostForExist(id);
        postMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        //检验
        validatePostForExist(id);
        //创建更新的实体类
        PostDO postObject = new PostDO();
        postObject.setStatus(status);
        postObject.setId(id);
        //更新
        postMapper.updateById(postObject);
    }

    @Override
    public PostRespVO getPostById(Long id) {
        //校验
        if(id==null)
            return null;
        //获取
        PostDO postDO = postMapper.selectById(id);
        //转化
        PostRespVO postRespVO= PostConvert.CONVERT.convert01(postDO);
        return postRespVO;
    }

    @Override
    public PageResult<PostDO> getPostPage(PostGetPageReqVO reqVO) {
        PageResult<PostDO> postDOPageResult=postMapper.selectPostPage(reqVO);
      return postDOPageResult;
    }

    @Override
    public List<PostDO> getPostSimpleList(String name) {
        List<PostDO> postDOList= postMapper.selectListByName(name);
       return postDOList;

    }


    private void validatePostForCreateOrUpdate(Long id, String code, String name) {
        //判断是否存在
        validatePostForExist(id);
        //判断授权码是否唯一
        validatePostForUniqueCode(id,code);
        //判断名字是否唯一
        validatePostForUniqueName(id,name);
    }
    //校验名字的唯一性
    private void validatePostForUniqueName(Long id, String name) {
        if(name==null){
            return;
        }
       PostDO postDO= postMapper.selectByName(name);
        if(postDO==null){
            return;
        }
        if(id==null){
            throw expection(POST_CODE_EXIST);
        }
        if(id.equals(postDO.getId())){
            throw expection(POST_CODE_EXIST);
        }
    }


    //判断post是否存在
    private void validatePostForExist(Long id) {
        if(id==null){
            return;
        }
        PostDO postDO = postMapper.selectById(id);
        if(postDO==null){
            throw expection(POST_NOT_EXIST);
        }
    }
    //判断授权码是否唯一
    private void validatePostForUniqueCode(Long id, String code) {
        //这个是为了更新的
        if(code==null){
            return;
        }
        PostDO postDO = postMapper.selectPostByCode(code);
        if(ObjectUtil.isNull(postDO)){
            return;
        }
        if(id==null){
            throw expection(POST_CODE_EXIST);
        }
        if(!id.equals(postDO.getId())){
            throw expection(POST_CODE_EXIST);
        }
    }
}
